﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public static class SearchNodeExtention
	{
		public static List<SearchNode> GetEndNodes(this SearchNode node)
		{
			if (node == null)
			{
				return null;
			}
			List<SearchNode> list = new List<SearchNode>();
			if (node.GetSubNodes() == null)
			{
				list.Add(node);
			}
			else
			{
				List<SearchNode> list2 = node.GetSubNodes();
				while (list2.Count > 0)
				{
					List<SearchNode> list3 = new List<SearchNode>();
					foreach (SearchNode searchNode in list2)
					{
						if (!searchNode.IsLoop)
						{
							if (searchNode.GetSubNodes() == null)
							{
								list.Add(searchNode);
							}
							else
							{
								list3.AddRange(searchNode.GetSubNodes());
							}
						}
					}
					list2 = list3;
				}
			}
			return list;
		}

		public static List<SearchNode> GetSubNodes(this SearchNode node)
		{
			if (node == null)
			{
				return null;
			}
			List<SearchNode> list = new List<SearchNode>();
			if (node.NextNode != null)
			{
				list.Add(node.NextNode);
			}
			if (node.BranchNode != null)
			{
				list.Add(node.BranchNode);
			}
			if (node.Branch2Node != null)
			{
				list.Add(node.Branch2Node);
			}
			if (list.Count == 0)
			{
				return null;
			}
			return list;
		}

		public static double CurFlow(this SearchNode node, List<ElementId> ids)
		{
			return (from n in node.GetEndNodes()
			where ids.Contains(n.Id)
			select n.EndNodeLoad("总散热量")).Sum();
		}

		public static double EndNodeFlow(this SearchNode node, string paraName = "水流量")
		{
			Element element = node.Element;
			if (element.Category.Id.IntegerValue != -2001140)
			{
				return 0.0;
			}
			Parameter parameter = element.GetParameter(paraName);
			if (parameter == null)
			{
				return 0.0;
			}
			return UnitConvert.FlowToInternal(parameter.AsDouble() * 60.0);
		}

		public static double EndNodeLoad(this SearchNode node, string paraName = "总散热量")
		{
			Element element = node.Element;
			if (element.Category.Id.IntegerValue != -2001140)
			{
				return 0.0;
			}
			Parameter parameter = element.GetParameter(paraName);
			if (parameter == null)
			{
				return 0.0;
			}
			return UnitConvert.CovertFromAPI((Autodesk.Revit.DB.DisplayUnitType)40, parameter.AsDouble());
		}

		public static bool IsRiserPipe(this SearchNode node)
		{
			if (node == null || !(node.Element is Pipe))
			{
				return false;
			}
			if (node.In == null || node.Out == null)
			{
				return false;
			}
			XYZ xyz = (node.In.Origin - node.Out.Origin).Normalize();
			return xyz.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)) || xyz.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0));
		}

		public static bool IsHorizontalPipe(this SearchNode node)
		{
			return node != null && node.In != null && node.Out != null && node.Element is Pipe && Math.Abs(node.In.Origin.Z - node.Out.Origin.Z) < 0.0001;
		}

		public static SearchElementType ElementType(this SearchNode node)
		{
			SearchElementType result = SearchElementType.OTHER;
			int integerValue = node.Element.Category.Id.IntegerValue;
			if (integerValue <= -2008049)
			{
				if (integerValue == -2008055)
				{
					return SearchElementType.VALVE;
				}
				if (integerValue == -2008049)
				{
					switch (node.ConnectorCount)
					{
					case 2:
						if (node.In.Radius > node.Out.Radius)
						{
							return SearchElementType.Narrow;
						}
						if (node.In.Radius < node.Out.Radius)
						{
							return SearchElementType.Expand;
						}
						return SearchElementType.ELBOW;
					case 3:
						return SearchElementType.TEE;
					case 4:
						return SearchElementType.CROSS;
					default:
						return result;
					}
				}
			}
			else
			{
				if (integerValue == -2008044)
				{
					return SearchElementType.PIPE;
				}
				if (integerValue == -2001140)
				{
					return SearchElementType.EQUIPMENT;
				}
			}
			result = SearchElementType.OTHER;
			return result;
		}

		public static string SystemClassName(this SearchNode node)
		{
			string result = string.Empty;
            if ((int)node.System.SystemInfo.SystemClass == 7)
			{
				result = "供水";
			}
            else if ((int)node.System.SystemInfo.SystemClass == 8)
			{
				result = "回水";
			}
			else
			{
				result = " ";
			}
			return result;
		}
	}
}
